package main

import (
	"container/list"
	"fmt"
)

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func levelOrderBottom(root *TreeNode) [][]int {
	result := make([][]int, 0)
	res := make([][]int, 0)
	queue := list.New()
	if root == nil {
		return result
	}
	queue.PushBack(root)
	for queue.Len() != 0 {
		tempArray := []int{}
		size := queue.Len()
		for i := 0; i < size; i++ {
			node := queue.Front().Value.(*TreeNode)
			queue.Remove(queue.Front())
			if node.Left != nil {
				queue.PushBack(node.Left)
			}
			if node.Right != nil {
				queue.PushBack(node.Right)
			}
			tempArray = append(tempArray, node.Val)
		}
		result = append(result, tempArray)
	}
	for i := 0; i < len(result); i++ {
		res = append(res, result[len(result)-i-1])
	}
	return res
}

func levelOrderBottom2(root *TreeNode) [][]int {

}
func main() {
	node1 := &TreeNode{Val: 1}
	node2 := &TreeNode{Val: 2}
	node3 := &TreeNode{Val: 3}
	node4 := &TreeNode{Val: 4}
	node5 := &TreeNode{Val: 5}
	node6 := &TreeNode{Val: 6}
	node7 := &TreeNode{Val: 7}
	node1.Left = node2
	node1.Right = node3

	node2.Left = node4
	node2.Right = node5

	node3.Left = node6
	node3.Right = node7
	res := levelOrderBottom(node1)
	fmt.Printf("res: %v\n", res)
}
